package com.leyou.gateway.config;

import com.leyou.common.auth.RsaUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.security.PublicKey;

@Data
@Slf4j
@ConfigurationProperties(prefix = "ly.jwt")
public class JwtProperties implements InitializingBean {
    /**
     * 公钥地址
     */
    private String pubKeyPath;
    /**
     * 公钥对象
     */
    private PublicKey publicKey;


    /**
     * 用户配置对象
     */
    private UserProperties user = new UserProperties();
    private ApplicationProperties app = new ApplicationProperties();
    private PrivilegeProperties privilege = new PrivilegeProperties();

    /**
     * 内部内加载useroken的一些属性
     */
    @Data
    public class UserProperties {
        private String cookieName;

    }

    /**
     * 内部加载微服务的一些属性
     */
    @Data
    public class ApplicationProperties {
        private Long id;
        private String secret;
    }

    /**
     * 加载网关携带token的一些属性
     */
    @Data
    public class PrivilegeProperties {
        private String headerName;
    }


    @Override

    public void afterPropertiesSet() throws Exception {
        try {
            publicKey = RsaUtils.getPublicKey(pubKeyPath);
        } catch (Exception e) {
            log.error("【网关微服务】初始化公钥失败 原因: {}", e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }
}